var form = layui.form,id=getQueryVariable("id");
var productCode=parent.code;
var fjCode=parent.fjCode;

var cgbz=getdictionary(5,14);//采购币种
var gys=getgys();


var cgbzselect=$("select[name='cgbz']");//采购币种
cgbzselect.append(getoption(cgbz));


var gysselect=$("select[name='code']");//供应商
gysselect.append(getoption(gys));

//$("input[name='catalog']").attr("readOnly",true);
//catalog
/*var catalogselect=$("select[name='catalog']");//采购币种
catalogselect.append(getoption(catalogAll));*/

//表单初始值，修改时调用
let myDate=new Date();
//时间处理 初始赋值
function getNowFormatDate() {
    let date = new Date();
    let separator = "-";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let nowDate = year + separator + month + separator + strDate;
    return nowDate;
}
let nowDate = getNowFormatDate();

if(id!=""){
    var oldData =  parent.table.cache["asuppliertableid"];
   /* form.val("editor-form",{
        "manufacturernumber":"进入修改",
    })*/
/*, {field: 'code', title: '备选供应商', sort: true}
, {field: 'cshh', title: '厂商货号', sort: true}
, {field: 'cgbz', title: '采购币种', sort: true}
, {field: 'cgcb', title: '采购成本', sort: true}
, {field: 'catalog', title: '所属目录', sort: true}*/
    $.each(oldData, function (index, value) {
        if (oldData[index]["id"] == id) {
            form.val("editor-form",{
                "code":oldData[index]["code"],
                "cshh":oldData[index]["cshh"],
                "cgbz":oldData[index]["cgbz"],
                "cgcb":oldData[index]["cgcb"],
                "catalog":oldData[index]["catalog"],
                "fob":oldData[index]["fob"],
                "updateDate":nowDate,
                "createDate":oldData[index]["createDate"],
            })
        }
    });
    form.render();
}else{
    form.val("editor-form",{
        "code":"",
        "cshh":0,
        "cgbz":"",
        "cgcb":0,
        "catalog":0,
        "fob":0,
        "updateDate":null,
        "createDate":nowDate,
    })
}
//监听提交
form.on('submit(form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(savegys(data.field)){
        layer.msg("保存成功", {icon: 6});
        parent.refrash_gys();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }else{
        layer.msg("保存失败", {icon: 6});
    }


    return false;
});
function  savegys(data) {
    if(id !="") data.id=id;
     data.productCode=productCode;
     data.fjCode=fjCode;
    // data.catalog=parent.cataCode;
    var tf=false;
    $.ajax({
        type : 'post',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'productfjgysbak/savegys',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf= da.data >0 ;
                // id=da.data;//
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}
//获取所有供应商
//获取合作类型，且审批通过 的供应商
function  getgys() {
    var gys=[];
    $.ajax({
        async:false,
        url: "purchase/getFilterCustomer2List",
        success: function (da) {
            if (da.status == 200) {
                for(var i=0;i<da.data.length;i++){
                    var obj={};
                    obj.code=da.data[i].code;
                    obj.name=da.data[i].name;
                    gys.push(obj);
                }
            }
        }
    });
    return gys;
}
function  getoption(objs) {
    var stoptions="<option value=''></option>";
    for(var i=0;i<objs.length;i++){
        stoptions+="<option value='"+objs[i].code+"'>"+objs[i].name+"</option>";
    }
    return stoptions;
}

function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}

//计算未含税出厂价
function changecgcb(){
    //含税出厂价
    let cgcb=$("#cgcb").val();
    let cgcbBig
    if (isNumber(cgcb)){
        cgcbBig=new BigNumber(cgcb);
    }else{
        cgcbBig=new BigNumber(0);
    }
    let catalog=cgcbBig.dividedBy(1.13).toFixed(4);
    $("#catalog").val(catalog);
    form.render();
}

//计算未含税FOB价格
function changeFob(){
    //含税出厂价
    let fob=$("#fob").val();
    let fobBig
    if (isNumber(fob)){
        fobBig=new BigNumber(fob);
    }else{
        fobBig=new BigNumber(0);
    }
    let cshh=fobBig.dividedBy(1.13).toFixed(4);
    $("#cshh").val(cshh);
    form.render();
}
